package com.nuatar.meibi.pubvar;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class LAdapter extends BaseAdapter {
	public List mlist;
	List<Integer> lstPosition = new ArrayList<Integer>();
	List<View> lstView = new ArrayList<View>();
	List<Integer> lstTimes = new ArrayList<Integer>();
	Context context;
	ViewBinder vbx;

	public LAdapter(Context ctx, List wp, ViewBinder vb) {
		mlist = wp;
		context = ctx;
		vbx = vb;
		

	}

	@Override
	public int getCount() {

		if (null != mlist) {
			return mlist.size();
		} else {
			return 0;
		}
	}
	public void Clear(){
		mlist.clear();
		lstPosition.clear();
		lstView.clear();
		lstTimes.clear();
		this.notifyDataSetChanged();
	}
	@Override
	public Object getItem(int position) {

		return mlist.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		 if (lstPosition.contains(position) == false) {
		 if(lstPosition.size()>200)//这里设置缓存的Item数量
		 {
		 lstPosition.remove(0);//删除第一项
		 lstView.remove(0);//删除第一项
		 }
		 convertView =vbx.getView(position, context,mlist.get(position));
		 lstPosition.add(position);//添加最新项
		 lstView.add(convertView);//添加最新项
		 } else
		 {
		 convertView = lstView.get(lstPosition.indexOf(position));
		 vbx.update(convertView, position,mlist.get(position));
		 }
		
		return convertView;
	}

	public interface ViewBinder {
		public View getView(int position, Context ctx, Object data);
		public View update(View v, int position,Object data);
	}
	
}
